package com.cy.src.upload;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import rx.Observable;
import rx.functions.Action1;

/**
 * Created by CY on 2016/10/27.
 */

public class UploadHelper {

    public static void main(String args[]) {
        OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(new Interceptor() {
                    @Override
                    public Response intercept(Chain chain) throws IOException {
                        Request request = chain.request();
                        Response response = chain.proceed(request);
                        String url = request.url().toString();
                        System.out.println("url=" + url);
                        return response;
                    }
                })
                .build();
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://192.168.253.141:5000")
                .client(client)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();


        UploadService uploadService = retrofit.create(UploadService.class);
        upload(uploadService);
    }

    private static void download(UploadService uploadService) {

        Observable<ResponseBody> download = uploadService.download("123456", "123.png");

        download.subscribe(new Action1<ResponseBody>() {
            @Override
            public void call(ResponseBody requestBody) {

                try {
                    byte[] bytes = requestBody.bytes();
                    File file = new File("C:\\User\\Administrator\\Pictures\\123_download.png");

                    FileOutputStream fos = new FileOutputStream(file);
                    fos.write(bytes);
                    fos.flush();
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }, new Action1<Throwable>() {
            @Override
            public void call(Throwable throwable) {
                throwable.printStackTrace();
            }
        });

    }

    private static void upload(UploadService uploadService) {


        File file = new File("C:\\Users\\Administrator\\Pictures\\湖北省环保厅-4_ 环评单位承接项目环评公示.png");
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);


        MultipartBody.Part part = MultipartBody.Part.createFormData("fileName", file.getName(), requestBody);

        Observable<ResponseBody> upload = uploadService.upload("123456", file.getName(), part);
        upload.subscribe(new Action1<ResponseBody>() {
            @Override
            public void call(ResponseBody requestBody) {
                try {
                    System.out.println("subscribe  requestBody =" + requestBody.string());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }, new Action1<Throwable>() {
            @Override
            public void call(Throwable throwable) {
                throwable.printStackTrace();
            }
        });

    }
}
